package Control;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.*;
import java.util.ArrayList;
import java.util.Date;
import Database.*;
import Model.*;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;

public class DeleteSupplier extends JFrame implements ActionListener
{
	private JLabel id;
	private JTextField name;
	private JButton search, clear, delete, cancel;
	private JPanel leftPanel, rightPanel;
	private TableOperations to;
	private JTable jt;
	private DefaultTableModel jtM;
	private ArrayList users;
	private ArrayList<Supplier> test = new ArrayList<Supplier>();
	private Supplier toBeEdited = null;
	private ImageIcon icon = new ImageIcon("frameIcon.png");

	public DeleteSupplier()
	{
		GridBagLayout layout = new GridBagLayout();
		GridBagConstraints gc = new GridBagConstraints();

		setIconImage(icon.getImage());
		setTitle("MDL - Delete Supplier");
		setSize(450, 375);
		setLocationRelativeTo(null);
		setResizable(false);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		this.setLayout(layout);
		to = new TableOperations();

		Insets insets = new Insets(15, 15, 13, 3);

		id = new JLabel("ID or Name");
		gc.gridx = 0;
		gc.gridy = 0;
		gc.weighty = 0;
		gc.weightx = 0;
		gc.insets = insets;
		gc.fill = GridBagConstraints.BOTH;
		add(id, gc);

		name = new JTextField(10);
		gc.gridx = 0;
		gc.gridy = 0;
		gc.weighty = 0;
		gc.weightx = 1;
		insets = new Insets(15, 100, 13, 15);
		gc.insets = insets;
		gc.fill = GridBagConstraints.BOTH;
		add(name, gc);

		search = new JButton("Search");
		search.addActionListener(this);
		gc.gridx = 0;
		gc.gridy = 1;
		gc.weighty = 0;
		gc.weightx = 1;
		insets = new Insets(0, 25, 0, 250);
		gc.insets = insets;
		gc.fill = GridBagConstraints.BOTH;
		add(search, gc);

		clear = new JButton("Clear");
		clear.addActionListener(this);
		gc.gridx = 0;
		gc.gridy = 1;
		gc.weighty = 0;
		gc.weightx = 1;
		insets = new Insets(0, 250, 0, 25);
		gc.insets = insets;
		gc.fill = GridBagConstraints.BOTH;
		add(clear, gc);

		gc.gridx = 0;
		gc.gridy = 2;
		gc.weighty = 0;
		gc.weightx = 0;
		insets = new Insets(15, 50, 0, 3);
		gc.insets = insets;
		gc.fill = GridBagConstraints.BOTH;
		add(new JLabel("Suppliers"), gc);

		createTable();
		gc.gridx = 0;
		gc.gridy = 3;
		gc.weighty = 1;
		gc.weightx = 3;
		insets = new Insets(5, 25, 15, 30);
		gc.insets = insets;
		gc.fill = GridBagConstraints.BOTH;
		add(new JScrollPane(jt), gc);

		delete = new JButton("Delete");
		delete.addActionListener(this);
		gc.gridx = 0;
		gc.gridy = 4;
		gc.weighty = 0;
		gc.weightx = 1;
		insets = new Insets(0, 25, 25, 250);
		gc.insets = insets;
		gc.fill = GridBagConstraints.BOTH;
		add(delete, gc);

		cancel = new JButton("Cancel");
		cancel.addActionListener(this);
		gc.gridx = 0;
		gc.gridy = 4;
		gc.weighty = 0;
		gc.weightx = 1;
		insets = new Insets(0, 250, 25, 25);
		gc.insets = insets;
		gc.fill = GridBagConstraints.BOTH;
		add(cancel, gc);
	}

	public void actionPerformed(ActionEvent e)
	{
		if (e.getSource() == cancel)
			this.dispose();
		else if (e.getSource() == clear)
			clear();
		else if (e.getSource() == search)
		{
			if (jtM.getRowCount() > 0)
			{
				clear();
				search();
			} else
				search();
		} else if (e.getSource() == delete)
		{
			if (to.deleteSupplier(toBeEdited))
			{
				JOptionPane.showMessageDialog(null, "Supplier deleted");
				clear();
			} else
				JOptionPane.showMessageDialog(null,
						"Supplier could not be deleted");
		}
	}

	public void createTable()
	{
		jtM = new DefaultTableModel();

		jtM.addColumn("Supplier ID");
		jtM.addColumn("Supplier Name");

		fillTable();

		jt = new JTable(jtM);
		jt.addMouseListener(new MouseAdapter()
		{
			public void mousePressed(MouseEvent e)
			{
				int row = jt.getSelectedRow();
				int supplierID = test.get(row).getSupplierID();
				String name = test.get(row).getName();
				toBeEdited = new Supplier(supplierID, name);
				System.out.println(supplierID);
			}
		});
	}

	public void clear()
	{
		try
		{
			jtM.removeRow(0);
			for (int i = 0; i < jtM.getRowCount(); i++)
			{
				jtM.removeRow(i);
			}

			test.remove(0);
			for (int i = 0; i < test.size(); i++)
			{
				test.remove(i);
			}
		} catch (Exception e)
		{
			System.out.println("No rows to be deleted");
		}
	}

	public void fillTable()
	{
		if (jtM.getRowCount() >= 1)
		{
			clear();

			String[] name =
			{ Integer.toString(test.get(test.size() - 1).getSupplierID()),
					test.get(test.size() - 1).getName() };
			jtM.addRow(name);

		} else
		{
			for (int i = 0; i < test.size(); i++)
			{
				String[] name =
				{ Integer.toString(test.get(i).getSupplierID()),
						test.get(i).getName() };
				jtM.addRow(name);
			}
		}
	}

	public void search()
	{
		try
		{
			test.addAll(to.searchSupplier(Integer.parseInt(name.getText())));
		} catch (NumberFormatException nfe)
		{
			test.addAll(to.searchSupplier(name.getText()));
		}
		fillTable();
		if (jtM.getRowCount() <= 0)
			JOptionPane
					.showMessageDialog(null,
							"Supplier not found. Hint: Use a CAPITAL LETTER for the 1st Letter");
	}
}